package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.HyrjAppointmentAction;
import com.ruoyi.system.service.IHyrjAppointmentActionService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.HyrjAppointmentMapper;
import com.ruoyi.system.domain.HyrjAppointment;
import com.ruoyi.system.service.IHyrjAppointmentService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-07
 */
@Service
public class HyrjAppointmentServiceImpl implements IHyrjAppointmentService {
    @Autowired
    private HyrjAppointmentMapper hyrjAppointmentMapper;

    @Autowired
    private ISysUserService userService;
    @Autowired
    private IHyrjAppointmentActionService hyrjAppointmentActionService;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public HyrjAppointment selectHyrjAppointmentById(Long id) {
        return hyrjAppointmentMapper.selectHyrjAppointmentById(id);
    }

    @Override
    public HyrjAppointment selectHyrjAppointmentByUserIdAndNo(HyrjAppointment hyrjAppointment) {
        return hyrjAppointmentMapper.selectHyrjAppointmentByUserIdAndNo(hyrjAppointment);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param hyrjAppointment 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<HyrjAppointment> selectHyrjAppointmentList(HyrjAppointment hyrjAppointment) {
        return hyrjAppointmentMapper.selectHyrjAppointmentList(hyrjAppointment);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param hyrjAppointment 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertHyrjAppointment(HyrjAppointment hyrjAppointment) {

        //一个人只能预约一台相同的洗衣机
        hyrjAppointment.setAppointmentStatus("1");
        HyrjAppointment hyrjAppointment1 = hyrjAppointmentMapper.selectHyrjAppointmentByUserIdAndNo(hyrjAppointment);

        if (null != hyrjAppointment1 && hyrjAppointment1.getAppointmentStatus().equals("1")) {
            return 0;
        }
        hyrjAppointment.setMachineNo(hyrjAppointment.getMachineNo());
        hyrjAppointment.setAppointmentTime(DateUtils.getNowDate());
        hyrjAppointment.setUpdateTime(DateUtils.getNowDate());
        hyrjAppointment.setCreateTime(DateUtils.getNowDate());
        hyrjAppointment.setAppointmentStatus("1");
        SysUser sysUser = userService.selectUserById(hyrjAppointment.getUserId());
        if (null != sysUser) {
            hyrjAppointment.setUserName(sysUser.getUserName());
        }
        HyrjAppointmentAction hyrjAppointmentAction = new HyrjAppointmentAction();
        BeanUtils.copyProperties(hyrjAppointment, hyrjAppointmentAction);
        hyrjAppointmentActionService.insertHyrjAppointmentAction(hyrjAppointmentAction);


        return hyrjAppointmentMapper.insertHyrjAppointment(hyrjAppointment);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param hyrjAppointment 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateHyrjAppointment(HyrjAppointment hyrjAppointment) {

        HyrjAppointment hyrjAppointment1 = hyrjAppointmentMapper.selectHyrjAppointmentById(hyrjAppointment.getId());

        if (null != hyrjAppointment1) {
            HyrjAppointmentAction hyrjAppointmentAction = new HyrjAppointmentAction();
            hyrjAppointmentAction.setUserId(hyrjAppointment1.getUserId());
            hyrjAppointmentAction.setMachineNo(hyrjAppointment1.getMachineNo());
            hyrjAppointmentAction.setAppointmentStatus("1");
            HyrjAppointmentAction hyrjAppointmentAction1 = hyrjAppointmentActionService.selectHyrjAppointmentActionByMachineNo(hyrjAppointmentAction);
            if (null != hyrjAppointmentAction1) {
                hyrjAppointmentActionService.deleteHyrjAppointmentActionById(hyrjAppointmentAction1.getId());
            }
        }

        return hyrjAppointmentMapper.updateHyrjAppointment(hyrjAppointment);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteHyrjAppointmentByIds(Long[] ids) {
        return hyrjAppointmentMapper.deleteHyrjAppointmentByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteHyrjAppointmentById(Long id) {
        return hyrjAppointmentMapper.deleteHyrjAppointmentById(id);
    }
}
